<!DOCTYPE html>
<html>
    <head id="Head1">
        <title>
            Tesdo Admin
        </title>
        <?php
        require_once dirname(__FILE__) . '/includes/head.php';
        ?>
        <script type="text/javascript">
            function call() {
                document.getElementById('hiddenMsg').value = editor.getData();
            }
            function my_confirm(id) {
                var r = confirm('Are you sure?');
                if (r === true) {
                    var url = 'articles.php?t=d&id=' + id;
                    window.location = url;
                }
            }
        </script>
    </head>
    <body>
        <?php
        include_once dirname(dirname(__FILE__)) . '/shared/common.php';
        require_once dirname(__FILE__) . '/includes/script.php';
        require_once dirname(__FILE__) . '/includes/header.php';
        include_once dirname(__FILE__) . '/includes/ckeditor.php';
        include_once dirname(__FILE__) . '/layers/dal/articles_dal.php';
        include_once dirname(__FILE__) . '/layers/dal/category_dal.php';

        if (isset($_POST['btnSave']) && $_POST['btnSave'] == 'Save') {
            if ((!isset($_POST['ddlJournals']) ||
                    !isset($_POST['ddlCategory']) ||
                    !isset($_POST['txtDescription']) ||
                    $_POST['ddlJournals'] == '' ||
                    $_POST['ddlCategory'] == '' ||
                    $_POST['txtDescription'] == '')) {
                ?>
                <script type="text/javascript">
                    alert('You need to fulfill the information!');
                </script> 
                <?php
            } else {
                $article = new article();
                $article->JournalsSubID = 0;
                $article->Description = $_POST['txtDescription'];
                $article->JournalID = $_POST['ddlJournals'];
                $article->CategoryID = $_POST['ddlCategory'];
                if (add_article($article) == 1) {
                    ?>
                    <script type="text/javascript">
                        alert('Added articles successfully!');
                        window.location = 'articles.php';
                    </script> 
                <?php } else { ?>
                    <script type="text/javascript">
                        alert('Failed to add articles!');
                    </script> 
                    <?php
                }
            }
        }
        //Check type and set $type value
        if (isset($_GET['t'])) {
            $t = $_GET['t'];
            if ($t == 'e')
                $type = 'edit';
            else
                $type = 'delete';
        }

        //Get ID
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
        }

        //Edit
        if (isset($type) && $type == 'edit') {
            $edit_article_old = get_journal_sub_detail_by_id($id);
        }

        //Delete
        if (isset($type) && $type == 'delete') {
            
        }
        ?>
        <div>
            <div class="content-box-outer">
                <div class="content-box-right">
                </div>
                <div class="content-box-left">
                    <form action="articles.php" method="POST" id="form1" onsubmit="call()" >
                        <h1>Journals Sub Detail</h1>
                        <div class="TitleArea"><b>Select Journal: </b></div>
                        <div class="textBoxArea">
                            <select name="ddlJournals" id="ddlJournals" style="width:300px;">
                                <?php
                                $list = load_article_list();
                                foreach ($list as $i => $item) {
                                    ?>
                                    <option value="<?php echo $item['JournalID'] ?>"
                                    <?php
                                    if (!empty($edit_article_old)) {

                                        if ($item['JournalID'] == $edit_article_old['JournalID']) {
                                            echo 'selected';
                                        }
                                    }
                                    ?>
                                            >
                                        <?php echo $item['Title'] ?> </option>
                                <?php }
                                ?>
                            </select>

                        </div>
                        <div class="TitleArea"><b>Select Area: </b></div>
                        <div class="textBoxArea">
                            <select name="ddlCategory" id="ddlCategory" style="width:300px;">
                                <script type="text/javascript">
                                    function filldata(value) {
                                        $.ajax({
                                            type: "GET",
                                            url: "URL_TO_PHP_FILE",
                                            data: "playerID=" + value,
                                            success: function(result) {
                                                $("select[name=ddlJournals]").val(result);
                                            }
                                        });
                                    }
                                </script>

                                <!--                                <option value="46">Aim and Scope</option>
                                                                <option value="55">Author’s Responsibilities</option>
                                                                <option value="52">Current Issue</option>
                                                                <option value="54">Editor’s Responsibilities</option>
                                                                <option value="58">Editorial Board</option>
                                                                <option value="51">Indexed / Abstracted</option>
                                                                <option value="57">Instructions for Referees</option>
                                                                <option value="50">Instructions for Submission</option>
                                                                <option value="49">Publication Charges</option>
                                                                <option value="53">Publication Ethics</option>
                                                                <option value="56">Responsibilities and Rights of Peer Reviewers</option>-->
                            </select>
                        </div>
                        <br>
                        <div class="TitleArea"><b>Description: </b></div>
                        <div style="margin-top: 80px;">
                            <input type="hidden" name="hiddenMsg" value="">
                            <textarea class="ckeditor" id="txtDescription" name="txtDescription">
                            </textarea>
                        </div>
                        <div class="textBoxArea" style="margin-bottom: 30px;">
                            <input type="submit" name="btnSave" value="Save" id="btnSave" class="buttonDesign">
                            <input type="reset" name="btnClear" value="Clear" id="btnClear" class="buttonDesign">
                        </div>
                    </form>
                    <br clear="all">
                    <h1>Manage Journals Sub Detail</h1>
                    <div>
                        <table cellspacing="0" cellpadding="4" rules="cols" id="cphAdmin_gvJournalSubDetail" style="color:Black;background-color:White;border-color:#DEDFDE;border-width:1px;border-style:None;width:988px;border-collapse:collapse;">
                            <tbody>
                                <tr style="color:White;background-color:#6B696B;font-weight:bold;">
                                    <th scope="col">Journal Title</th><th scope="col">Category</th><th scope="col">Description</th><th scope="col">Actions</th>
                                </tr>
                                <?php
//                                $all_journal_sub_detail = get_all_journal_sub_detail();
                                $current_page = 1;
                                $page_size = 10;
                                if (!empty($_GET['p'])) {
                                    $current_page = $_GET['p'];
                                }
                                $all_journal_sub_detail = get_current_page_list($current_page, $page_size);
                                $count = 0;
                                foreach ($all_journal_sub_detail as $i => $item) {
                                    if ($count % 2 == 0) {
                                        ?>
                                        <tr style="background-color:#F7F7DE;">
                                            <td><?php echo $item['Title']; ?></td>
                                            <td style="width:200px;"><?php echo $item['catTitle']; ?></td>
                                            <td><?php echo substr(strip_tags($item['Description']), 0, 200); ?></td>
                                            <td align="center">
                                                <a href="articles.php?t=e&id=<?php echo $item['JournalsSubID'] ?>">
                                                    <img src="layout/images/edit.png" style="width:15px; outline: none;" />
                                                </a>
                                                <input type="image" 
                                                       name="ImageButtonDelete<?php echo $count; ?>" 
                                                       id="ImageButtonDelete<?php echo $count; ?>" 
                                                       title="Delete" 
                                                       src="layout/images/delete.png" 
                                                       onclick="return my_confirm(<?php echo $item['JournalsSubID']; ?>);" style="width:15px;">
                                            </td>
                                        </tr>
                                    <?php } else { ?>
                                        <tr style="background-color:#FFFFFF;">
                                            <td><?php echo $item['Title']; ?></td>
                                            <td style="width:200px;"><?php echo $item['catTitle']; ?></td>
                                            <td><?php echo substr(strip_tags($item['Description']), 0, 200); ?></td>
                                            <td align="center">
                                                <a href="articles.php?t=e&id=<?php echo $item['JournalsSubID'] ?>">
                                                    <img src="layout/images/edit.png" style="width:15px; outline: none;" />
                                                </a>
                                                <input type="image" 
                                                       name="ImageButtonDelete<?php echo $count; ?>" 
                                                       id="ImageButtonDelete<?php echo $count; ?>" 
                                                       title="Delete" 
                                                       src="layout/images/delete.png" 
                                                       onclick="return my_confirm(<?php echo $item['JournalsSubID']; ?>);" style="width:15px;">
                                            </td>
                                        </tr>
                                        <?php
                                    }
                                    $count++;
                                }
                                ?>
                                <tr align="right" style="color:Black;background-color:#F7F7DE;">
                                    <td colspan="4">
                                        <table>
                                            <tbody>
                                                <tr>
                                                    <?php
                                                    $total_page = get_total_page($page_size);
                                                    for ($i = 0; $i < round($total_page); $i++) {
                                                        $p = $i + 1;
                                                        ?>
                                                        <td>
                                                            <span>
                                                                <a href="articles.php<?php
                                                                $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
                                                                if (false !== strpos($url, '?')) {
                                                                    if (empty($_GET['p'])) {
                                                                        echo '?' . $_SERVER['QUERY_STRING'] . '&p=' . $p;
                                                                    } else {
                                                                        $old_query = $_SERVER['QUERY_STRING'];
                                                                        $new_query = substr($old_query, 0, strpos($old_query, '&p'));
                                                                        echo '?' . $new_query . '&p=' . $p;
                                                                    }
                                                                } else {
                                                                    echo '?p=' . $p;
                                                                }
                                                                ?>">
                                                                       <?php echo $p; ?>
                                                                </a>
                                                            </span>
                                                        </td>
                                                    <?php } ?>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="MessageArea">
                    <span id="cphAdmin_lblMsg"></span>
                </div>
            </div>

        </div>

        <?php
        require_once dirname(__FILE__) . '/includes/footer.php';
        ?>
    </body>
</html>